<template>
	<view>
		<web-view :src="src" @load="bindload" @error="binderror" @message="bindMessage">
		</web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: '',
				userInfo: {},
				remark: {}
			}
		},
		onLoad(options) {
			this.userInfo = uni.getStorageSync('userInfo')
			this.src = 'https://h5-faceverify.baidu.com/face/print/?' + decodeURIComponent(options.url)
			this.at = options.at
			this.vt = options.vt
		},
		methods: {
			bindload(e) {
				console.log(e, '加载');
				if (e.detail.src == 'https://psq.yoojet.com/#/pass/facePassLoading') {
					uni.request({
						url: "https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/detail?access_token=" +
							this.at,
						method: 'POST',
						data: {
							verify_token: this.vt
						}
					}).then(result => {
						let [err, res] = result;
						console.log(res.data);
						if (res.data.success) {
							this.certification(
								res.data.result.idcard_confirm.idcard_number,
								res.data.result.idcard_confirm.name,
								JSON.stringify(res.data),
								this.userInfo.phone
							);
						} else {
							uni.showToast({
								title: '验证失败,请尝试验证',
								icon: 'none'
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/index/index'
								})
							}, 1000)
						}
					})
				} else if (e.detail.src == 'http://msq.yoojet.com/error') {
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}, 1000)
				}
			},
			binderror(e) {
				console.log(e)
			},
			bindMessage(e) {
				//如果通过消息机制，可以在这里处理数据，和进行页面跳转逻辑
				console.log('接收到的消息：' + JSON.stringify(e.detail.data));
			},
			// 保存认证记录
			certification(idNumber, name, remark, phone) {
				uni.request({
					url: this.$veriUrl + '/auth/VeriRecord',
					method: 'POST',
					data: {
						phone,
						name,
						idNumber,
						state: true,
						VeriType: 1,
						remark
					}
				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						uni.showToast({
							title: '验证成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}, 1000)
					} else {
						uni.showToast({
							title: '验证失败,请尝试验证',
							icon: 'none'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}, 1000)
					}
				})
			},
		}
	}
</script>

<style>

</style>
